#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import sys,platform,os
curMod=sys.modules[__name__]
def ROOT():
    if os.path.islink(__file__):
        ROOT=os.path.dirname(os.readlink(__file__))
    else:
        ROOT=os.path.dirname(__file__)
    sys.path.append(os.path.abspath(ROOT+"/libs"))   


isPy2=(sys.version[0]=="2")
isPy3=(sys.version[0]=="3")

cmds=[
    "idocker 安装docker"
]
desc='''
usage: operation tools
shell脚本可以单独使用,也可以通过i.py使用

1. idocker 安装docker
2. irepo 安装repo
'''
arch=sys.platform[:5].lower()
funcPrefixs={"darwi":"m","ubuntu":"u","centos":"c","debian":"d"}
release=""
defaultFuncPrefix="l"
if arch=="linux":
    release=platform.linux_distribution()[0][:6].lower()
    funcPrefix=release[0]
elif arch=="darwi":
    release="darwin"
    funcPrefix="m"

print(release,"python:%s" % sys.version,)
def _p(args):
    if isPy2:
        print(args)
    else:
        print(args)
def _warn(args):
    print("\033[1;31;40m"+args+"\033[0m") 
def _info(args):
    print("\033[1;32;40m"+args+"\033[0m") 


def _sh(args):
    sh=release[0:2]+".sh"
    shellDir='shell'
    caller=(sys._getframe(1).f_code.co_name)
    if os.path.exists("./"+sh):
        os.system('cd %s;bash %s %s %s' % (shellDir,sh,caller,str.join(" ",args)))
    else:
        _warn("not exists "+sh)     


def idocker(args):
    _sh(args)
def irepo(args):
    _sh(args)

def usage():
    _info(desc)
    
if funcPrefix=="" and len(sys.argv)>1:
    error='''
warn: not find func prefix on %s 
'''    
    _warn(error % (platform.platform()))    
def _callFunc(args):

    if len(args)==1:
        return usage()
    funcName=args[1]  
    if hasattr(curMod,funcName):        
        return getattr(curMod,funcName)(args[1:])
    else:
        _warn("not exists this function %s \n" % funcName)    
_callFunc(sys.argv)
